//
//  GuideViewController.m
//  OurTourism
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "GuideViewController.h"
#import "ZJFtitleView.h"
#import "ZJFCollectionWithTableViewCell.h"
#import "MZNetHandle.h"
#import "ZJFguiDeModel.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "ZJFSecondViewController.h"
#import "ZJFMoTaiViewController.h"
@interface GuideViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,buttonClickChangePage,ChuanZhiDelegate>
@property (nonatomic, retain)ZJFtitleView *titleView;
@property (nonatomic, assign)NSInteger key;
@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *dataArray;
@property (nonatomic, retain)NSMutableArray *modelNewDataArr; //最新页对象数组
@property (nonatomic, retain)NSMutableArray *modelHotDataArr;
@property (nonatomic, retain)NSMutableArray *modelAsialDataArr;
@property (nonatomic, retain)NSMutableArray *modelOuzhouDataArr;
@property (nonatomic, retain)NSMutableArray *modelMeizhouDataArr;
@property (nonatomic, retain)NSMutableArray *modelFeizhouDataArr;
@property (nonatomic, retain)NSMutableArray *modelDayangzhouDataArr;
@property (nonatomic, retain)NSMutableArray *urlArr;
@property (nonatomic, strong)UITabBarItem *tabbar;
@property (nonatomic, strong)NSMutableArray *titleArr;
@end

@implementation GuideViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.navigationController.navigationBar.translucent = NO;//
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"攻略";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:62 / 255. green:176 / 255. blue:192 / 255. alpha:1];
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.94 alpha:1];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:nil style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem.title = @"筛选";
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    self.urlArr = [NSMutableArray array];
    self.titleArr = [NSMutableArray array];
    [self loadMBProgress];
    [self getNewData];

}
#pragma mark - 传值方法
- (void)transmitValue:(NSMutableArray *)returnTitleArr
{
    self.titleArr = returnTitleArr;
}
#pragma mark - 右按钮点击放法
- (void)rightAction
{
    ZJFMoTaiViewController *motai = [[ZJFMoTaiViewController alloc]init];
    motai.delegate = self;
   
    motai.block = ^(NSMutableArray *array) {
#warning 更新头部数组
        self.key = 0;
        self.urlArr = array;
//        NSMutableArray *arr = [NSMutableArray array];
//        arr = self.urlArr;
//
//        if ([self.urlArr[0] isEqualToString:@"1"]) {
//            [self.urlArr removeAllObjects];
//        }
//        for (NSInteger i = 0 ; i < arr.count - 1; i++) {
//            [self.urlArr addObject:arr[i + 1]];
//        }
      //  NSLog(@"%@",self.urlArr);
        [self parseData];
    };
    [self presentViewController:motai animated:YES completion:^{
        
    }];
}

#pragma mark - 获取最新页数据
- (void)getNewData
{
    //7个界面的api
    NSString *url1 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=9927&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=f448fd13669fbd2e6df0ff7ac1e1356d";//最新
    
    NSString *url2 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=17431&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=70167f27a9e8c78482cd40b158643394";//热门
    
    NSString *url3 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23213&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=13bb1c5771fc2b1a97bc31402b6afd88";//亚洲
    
    NSString *url4 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23423&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=1cb0345d6bee388e565af00571b3f71a"; //欧洲
    
    NSString *url5 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23647&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=a3a627ca7c167a09e420889c5fcb2abf"; //美洲
    
    NSString *url6 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23549&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=761bdfadd0a6c09f7552bdaf09d4ac11"; //非洲
    
    NSString *url7 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23339&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=95a73c75f1b83fb2b66a17dde471b7ca"; //大洋洲
    self.urlArr = [@[url1, url2, url3, url4, url5, url6, url7] mutableCopy]; //url数组
    [self parseData];
    
}

- (void)gata
{
    NSString *url1 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=9927&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=f448fd13669fbd2e6df0ff7ac1e1356d";//最新
    NSString *url2 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=17431&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=70167f27a9e8c78482cd40b158643394";//热门
    
    
    NSString *url3 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23213&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=13bb1c5771fc2b1a97bc31402b6afd88";//亚洲
    
    NSString *url4 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23423&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=1cb0345d6bee388e565af00571b3f71a"; //欧洲
    NSString *url5 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23647&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=a3a627ca7c167a09e420889c5fcb2abf"; //美洲
    NSString *url6 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23549&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=761bdfadd0a6c09f7552bdaf09d4ac11"; //非洲
    NSString *url7 = @"http://app6.117go.com/demo27/php/interestAction.php?start=&length=12&submit=getDiscoverDir&pid=23339&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=95a73c75f1b83fb2b66a17dde471b7ca"; //大洋洲
    self.urlArr = [@[url1, url2, url3, url4, url5, url6, url7] mutableCopy]; //url数组
}

#pragma mark - 创建头部scrollView
- (void)createTitleScrollView
{
   // NSLog(@"111111%@",self.titleArr);
    
    for (NSInteger i = 0 ; i < self.titleArr.count; i++) {
        if ([self.titleArr[0] isEqualToString:@"2"]) {
            self.titleArr = [@[@"最新", @"热门", @"亚洲", @"欧洲", @"美洲", @"非洲", @"大洋洲"] mutableCopy];
        }
    }
    
        if (self.titleArr.count == 0) {
        self.titleArr = [@[@"最新", @"热门", @"亚洲", @"欧洲", @"美洲", @"非洲", @"大洋洲"] mutableCopy];
    }
    self.titleView = [[ZJFtitleView alloc] initWithFrame:CGRectZero titleArr:self.titleArr];
    [self.view addSubview:self.titleView];
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(CWIDTH, 50));
    }];
    //签协议
    self.titleView.delegate = self;
    self.titleView.tag = 4000;
}
#pragma mark - 数据解析
- (void)parseData {
    
    for (NSInteger i = 0 ; i < self.urlArr.count ; i++) {
        if ([self.urlArr[0] isEqualToString:@"1"]) {
            [self gata];
        }
    }
    
    if (self.urlArr.count == 0) {
        [self gata];
    }
    
    for (NSString *url in self.urlArr) {
        //  [MZNetHandle getURL:url cookie:nil result:^(id result) {
        [MZNetHandle getUrl:url cookie:nil result:^(id result) {
            
            // }];
            NSMutableDictionary *dic = result;
            
            NSMutableArray *tempArr =  dic[@"obj"][@"list"];
            self.dataArray = [NSMutableArray array];
            for (NSMutableDictionary *dic in tempArr) {
                for (NSMutableDictionary *tempDic in dic[@"list"]) {
                    [self.dataArray addObject:tempDic];  //每一个tableView总数据
                }
            }
            //依次判断 以防止每个接口请求速度不同  导致请求到数据顺序混乱
            if ([url isEqualToString:self.urlArr[0]]) {
                self.modelNewDataArr = [ZJFguiDeModel transformWithArray:self.dataArray];
                
            }
            
            else if ([url isEqualToString:self.urlArr[1]])
            {
                self.modelHotDataArr = [ZJFguiDeModel transformWithArray:self.dataArray];
            }
            else if ([url isEqualToString:self.urlArr[2]])
            {
                self.modelAsialDataArr = [ZJFguiDeModel transformWithArray:self.dataArray];
            }
            else if ([url isEqualToString:self.urlArr[3]])
            {
                self.modelOuzhouDataArr = [ZJFguiDeModel transformWithArray:self.dataArray];
            }
            else if ([url isEqualToString:self.urlArr[4]])
            {
                self.modelMeizhouDataArr = [ZJFguiDeModel transformWithArray:self.dataArray];
            }
            else if ([url isEqualToString:self.urlArr[5]])
            {
                self.modelFeizhouDataArr = [ZJFguiDeModel transformWithArray:self.dataArray];
            }
            else if ([url isEqualToString:self.urlArr[6]])
            {
                self.modelDayangzhouDataArr = [ZJFguiDeModel transformWithArray:self.dataArray];
            }
            
            //记录最后一个url获取成功 在创建
            self.key++;
            if (self.key == self.urlArr.count) {
                
                [self.hud removeFromSuperview];
                [self.collection reloadData];
                [self createTitleScrollView];
                [self createMainScrollView];

            }
        }];
    }
}

#pragma mark - titleScrollView代理协议方法
- (void)ChangePage:(UIButton *)btn
{
    //点击button时  先让其他button变成默认状态
    for (NSInteger i = 0; i < 7; i++) {
        UIButton *btn = (UIButton *)[self.titleView viewWithTag:3000 + i];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
    }
    //collectionView的偏移量
    //改变当前button的状态
    self.collection.contentOffset = CGPointMake(CWIDTH * (btn.tag - 3000), 0);
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
}

#pragma mark - 滚啊滚
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.collection) {
        //通过偏移量记录button的tag值
        NSInteger i = self.collection.contentOffset.x / CWIDTH;
        
        for (NSInteger i = 0; i < 7; i++) {
            UIButton *btn = (UIButton *)[self.titleView viewWithTag:3000 + i];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:15];
            
        }
        if (i == 5) {
            self.titleView.scrollView.contentOffset = CGPointMake(170, 0);
        }
        if (i == 1) {
            self.titleView.scrollView.contentOffset = CGPointMake(0, 0);
        }
        
        ((UIButton *)[self.titleView viewWithTag:3000 + i]).titleLabel.font = [UIFont systemFontOfSize:17];
        [((UIButton *)[self.titleView viewWithTag:3000 + i]) setTitleColor:[UIColor colorWithRed:0.71 green:0.78 blue:0.73 alpha:1] forState:UIControlStateNormal];
        
        
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionAllowAnimatedContent animations:^{
            
            [self.titleView viewWithTag:3200].frame = CGRectMake(CWIDTH / 5 * i + 25, 45, 40, 2);
            
            
        } completion:^(BOOL finished) {
            
        }];
        
    }
}
#pragma mark - 创建collectionView
- (void)createMainScrollView
{
    //创建一个瀑布流
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(CWIDTH, CHEIGHT + 50);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //section距离collection的边距
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //item边距
    
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    //创建collectionView
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 60, CWIDTH , CHEIGHT - 50 ) collectionViewLayout:layout];
    [self.view addSubview:self.collection];
    self.collection.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    // self.collection.showsHorizontalScrollIndicator = YES;
    //整页滑动
    self.collection.pagingEnabled = YES;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    //注册cell
    [self.collection registerClass:[ZJFCollectionWithTableViewCell class] forCellWithReuseIdentifier:@"item"];
}
#pragma mark - colloction 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
       return self.urlArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZJFCollectionWithTableViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    
    if (indexPath.item == 0) {
        cell.dataArr = self.modelNewDataArr;
        
        cell.webViewArr = self.modelNewDataArr;
    }
    else if (indexPath.item == 1)
    {
        cell.dataArr = self.modelHotDataArr;
    }
    else if (indexPath.item == 2)
    {
        cell.dataArr = self.modelAsialDataArr;
    }
    else if (indexPath.item == 3)
    {
        cell.dataArr = self.modelOuzhouDataArr;
    }
    else if (indexPath.item == 4)
    {
        cell.dataArr = self.modelMeizhouDataArr;
    }
    else if (indexPath.item == 5)
    {
        cell.dataArr = self.modelFeizhouDataArr;
    }
    else if (indexPath.item == 6)
    {
        cell.dataArr = self.modelDayangzhouDataArr;
    }
    
    cell.item = indexPath.row;
    __weak typeof(self) weakself = self;//强引用改弱引用
    
    cell.block = ^(NSNumber *itemid){
        
        ZJFSecondViewController *location = [[ZJFSecondViewController alloc]init];
        location.hidesBottomBarWhenPushed = YES;
        location.itemid = itemid;
        [weakself.navigationController pushViewController:location animated:YES];
    };
    return cell;
}

#pragma mark - 菊花
- (void)loadMBProgress
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
    self.hud.dimBackground = NO;
    //设置对话框文字
    self.hud.labelText = @"loading";
    self.hud.minShowTime = 50;
    self.hud.activityIndicatorColor = [UIColor whiteColor];
    self.hud.labelColor = [UIColor whiteColor];
    
    [self.view addSubview:self.hud];
    [self.hud showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(1);
    } completionBlock:^{
        //操作执行完后取消对话框
        [self.hud removeFromSuperview];
        self.hud = nil;
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
